//
//  FHWelcomeViewController.swift
//  WeiBo
//
//  Created by qfh on 16/7/12.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import SDWebImage

let SwitchRootVCNotification = "SwitchRootVCNotification"


class FHWelcomeViewController: UIViewController {
    
    /// 背景图片
    lazy var bgImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return imageView
        
    
    }()
    
    /// 头像图片
    lazy var headImageView: UIImageView = {
    
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let  imgUrl = FHUserAccessViewModel.sharedUserAccessViewModel.userAccess?.avatar_large {
            
            imgView.sd_setImageWithURL(NSURL(string: imgUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        
        
        imgView.layer.cornerRadius = 42.5
        
        imgView.layer.masksToBounds = true
        
        
        
        
        return imgView
    
    }()
    /// 欢迎信息label
    lazy var messageLabel: UILabel = {
        
        
        let label = UILabel()
        
        var labText = FHUserAccessViewModel.sharedUserAccessViewModel.userAccess?.name
    
        label.text = "欢迎回来！\(labText ?? "")"
        
        label.textColor = UIColor.grayColor()
        
        label.textAlignment = .Center
        
        label.alpha = 0
        
        return label
    }()
    
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setUpUI()
       
        
        
    }
    
     override func viewDidAppear(animated: Bool) {
        
        
        super.viewDidAppear(animated)
        
        starAnimation()
    }
    
    
    /**
     加载ui
     */
    func setUpUI() {
    
    
        view.addSubview(headImageView)
        
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(view).offset(250)
            
            make.size.equalTo(CGSizeMake(85, 85))
            
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            
            
        }
        
        
    }
    
    /**
     开始动画
     */
    func starAnimation() {
        
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
        
           
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        print("动画完成")
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: self)
                        
                        
                })
        }
    
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
